import 'package:flutter/material.dart';
import 'package:flutterproject/mvvm/base/base_state.dart';
import 'package:flutterproject/page/mine/children/assets/childdren/charge_page.dart';
import 'package:flutterproject/page/mine/children/assets/childdren/yun_charge/index.dart';
import 'package:flutterproject/page/mine/children/assets/my_assets_page.dart';
import 'package:flutterproject/page/mine/children/my_group/my_group_page.dart';
import 'package:flutterproject/page/mine/children/order/order_list_page.dart';
import 'package:flutterproject/page/mine/controller/mine_controller.dart';
import 'package:flutterproject/page/quota/quota_page.dart';
import 'package:flutterproject/page/setting/setting_page.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/extension/widget_extension.dart';
import 'package:flutterproject/util/margin_padding_util.dart';
import 'package:flutterproject/util/sp_util/shared_preferences.dart';
import 'package:flutterproject/util/sp_util/sp_key.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:get/get.dart';

class MinePage extends BaseState<MineController> {
  @override
  MineController initController() => MineController();

  @override
  Widget initView(BuildContext context) {
    return ColoredBox(
      color: Color(0xFFF6F6F6),
      child: SingleChildScrollView(
        child: Column(
          children: [
            Stack(
              children: [
                ImageCommon(
                  "/image/main_bg",
                  width: 720.width,
                  height: 278.height,
                ),
                Positioned(
                    top: 75.height,
                    right: 40.width,
                    child: Row(
                      children: [
                        Column(
                          children: [
                            ImageCommon(
                              "/image/ic_maket",
                              size: 35.sp,
                            ),
                            TextCommon(
                              "行情",
                              size: 22.sp,
                              lineHeight: 28.sp,
                              color: Color(0xFF664918),
                            )
                          ],
                        ).addClickEvent(() => Get.to(QuotaPage())),
                        42.widthBox,
                        Column(
                          children: [
                            ImageCommon(
                              "/icon/setting",
                              size: 35.sp,
                            ),
                            TextCommon(
                              "设置",
                              size: 22.sp,
                              lineHeight: 28.sp,
                              color: Color(0xFF664918),
                            )
                          ],
                        ).addClickEvent(() => Get.to(SettingPage())),
                      ],
                    )),
                Container(
                    margin: MarginPaddingUtil.only(top: 190),
                    width: Get.width,
                    child: Stack(
                      clipBehavior: Clip.none,
                      children: [
                        ImageCommon(
                          "/image/mine_bg",
                          width: Get.width,
                          height: 280.width,
                        ),
                        Positioned(
                          top: -42.width,
                          left: 57.width,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                crossAxisAlignment: CrossAxisAlignment.end,
                                children: [
                                  Container(
                                    width: 120.width,
                                    height: 120.width,
                                    alignment: Alignment.center,
                                    decoration: BoxDecoration(
                                      shape: BoxShape.circle,
                                      border: Border.all(
                                        color: Colors.white,
                                        width: 7.width,
                                      ),
                                      image: DecorationImage(
                                        image: AssetImage(
                                            'assets/image/mine_vip_lever.png'),
                                        fit: BoxFit.fill, // 完全填充
                                      ),
                                    ),
                                    child: TextCommon(
                                      "${controller.mineIndexBean?.level ?? 0}",
                                      size: 57.sp,
                                      color: Colors.white,
                                      fontWeight: FontWeight.bold,
                                    ),
                                  ),
                                  16.widthBox,
                                  Container(
                                    margin: MarginPaddingUtil.only(bottom: 19),
                                    padding: MarginPaddingUtil.only(
                                        left: 5, right: 22),
                                    decoration: BoxDecoration(
                                      color: Color(0xFFF2B962),
                                      borderRadius: BorderRadius.circular(50),
                                      border: Border.all(
                                        color: Color(0xFFFFFDC2),
                                        width: 2.width,
                                      ),
                                    ),
                                    child: Row(
                                      children: [
                                        ImageCommon(
                                          "/icon/vip",
                                          size: 31.width,
                                        ),
                                        10.widthBox,
                                        TextCommon(
                                          controller.getVIPLevel(),
                                          size: 20.sp,
                                          fontWeight: FontWeight.w500,
                                          color: Colors.white,
                                          lineHeight: 29.sp,
                                        )
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                              20.heightBox,
                              TextCommon(
                                SPUtils()
                                    .getString(SPKey.mobile)
                                    .replaceFirst(RegExp(r'\d{4}'), '****', 3),
                                size: 36.sp,
                                fontWeight: FontWeight.bold,
                              )
                            ],
                          ),
                        ),
                        Positioned(
                            right: 0,
                            top: -85.width,
                            child: ImageCommon(
                              "/image/mine_vip",
                              size: 220.width,
                            )),
                        Positioned(
                          bottom: 30.width,
                          left: 57.width,
                          child: Row(
                            children: [
                              TextCommon(
                                "邀请码：",
                                color: Color(0xFF969696),
                              ),
                              3.widthBox,
                              TextCommon(
                                controller.mineIndexBean?.shareCode ?? "",
                                color: Color(0xFF969696),
                              ),
                              30.widthBox,
                              ImageCommon(
                                "/icon/copy",
                                size: 23.width,
                              ).addClickEvent(() => controller.copyInviteCode())
                            ],
                          ),
                        )
                      ],
                    )),
              ],
            ),
            Container(
              width: Get.width,
              margin: MarginPaddingUtil.symmetric(horizontal: 20),
              decoration: BoxDecoration(
                image: DecorationImage(
                  image: AssetImage('assets/image/mine_assets.png'),
                  fit: BoxFit.fill, // 完全填充
                ),
              ),
              child: Stack(
                children: [
                  Positioned(
                    top: 19.width,
                    right: 28.width,
                    child: Container(
                      padding: MarginPaddingUtil.fromLTRB(
                          left: 21, top: 9, right: 12, bottom: 12),
                      decoration: BoxDecoration(
                          color: Color(0xFFF5DAAF),
                          borderRadius: BorderRadius.circular(50)),
                      child: Row(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          TextCommon(
                            "资产列表",
                            size: 24.sp,
                            lineHeight: 35.sp,
                          ),
                          9.widthBox,
                          ImageCommon(
                            "/icon/mine_right_arrow",
                            size: 34.width,
                          )
                        ],
                      ),
                    ).addClickEvent(
                      () => _jumpAssetsPage(0),
                    ),
                  ),
                  Container(
                    margin: MarginPaddingUtil.only(top: 47, left: 140),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        TextCommon(
                          "我的资产",
                          size: 20.sp,
                          fontWeight: FontWeight.bold,
                          color: Color(0xFF4D4C4C),
                        ),
                        Row(
                          children: [
                            TextCommon(
                              "￥",
                              size: 28.sp,
                              fontWeight: FontWeight.bold,
                              lineHeight: 50.sp,
                            ),
                            8.widthBox,
                            TextCommon(
                              "${controller.myAssetsBean?.totalAmount?.toFixed(2)}",
                              size: 40.sp,
                              fontWeight: FontWeight.bold,
                              lineHeight: 58.sp,
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
                  Container(
                    width: Get.width,
                    padding: MarginPaddingUtil.fromLTRB(
                        left: 20, top: 40, right: 20, bottom: 17),
                    margin: MarginPaddingUtil.only(
                        top: 144, left: 26, right: 25, bottom: 20),
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(20.width),
                    ),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            _buildScoreItem(
                              title: "酝积分",
                              image: "mine_score",
                              color: Color(0xFFEFEDE5),
                              value:
                              "${controller.myAssetsBean?.list?[0].balance?.toFixed(2)}",
                              callback: () => _jumpAssetsPage(0),
                            ),
                            10.widthBox,
                            _buildScoreItem(
                              title: "创投积分",
                              image: "chuangtou",
                              color: Color(0xFFEFEDE5),
                              value:
                              "${controller.myAssetsBean?.list?[4].balance?.toFixed(2)}",
                              callback: () => _jumpAssetsPage(4),
                            ),
                          ],
                        ),
                        12.heightBox,
                        Row(
                          children: [
                            _buildScoreItem(
                              title: "绿色积分",
                              image: "scroe_green",
                              color: Color(0xFFF6EBEB),
                              value:
                                  "${controller.myAssetsBean?.list?[1].balance?.toFixed(2)}",
                              callback: () => _jumpAssetsPage(1),
                            ),
                            6.widthBox,
                            _buildScoreItem(
                              title: "权益积分",
                              image: "score_red",
                              color: Color(0xFFF6EBEB),
                              value:
                                  "${controller.myAssetsBean?.list?[2].balance?.toFixed(2)}",
                              callback: () => _jumpAssetsPage(2),
                            ),
                            6.widthBox,
                            _buildScoreItem(
                              title: "众商通",
                              image: "score_yellow",
                              color: Color(0xFFF6EBEB),
                              value:
                                  ((controller.myAssetsBean?.list?[3].balance ??
                                              0.000) +
                                          (controller.myAssetsBean?.list?[3]
                                                  .frozen ??
                                              0.000))
                                      .toFixed(2),
                              callback: () => _jumpAssetsPage(3),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  Positioned(
                    top: 33.width,
                    child: ImageCommon(
                      "/image/mine_coin",
                      width: 146.width,
                      height: 142.width,
                    ),
                  ),
                ],
              ),
            ),
            Container(
              margin: MarginPaddingUtil.only(top: 20, left: 20, right: 20),
              padding: MarginPaddingUtil.fromLTRB(
                  left: 31, top: 20, right: 31, bottom: 35),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20.width),
              ),
              child: Column(
                children: [
                  _buildTitle(
                    img: "order",
                    title: "我的订单",
                    color: Color(0xFFB7D892),
                    rightIcon: "mine_right2",
                    rightStr: "订单详情",
                  ).addClickEvent(() => Get.to(OrderListPage(), arguments: 0)),
                  51.heightBox,
                  Row(
                    children: [
                      14.widthBox,
                      Expanded(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            _buildImageText(
                              title: "待付款",
                              image: "1",
                              callback: () =>
                                  Get.to(OrderListPage(), arguments: 0),
                            ),
                            _buildImageText(
                              title: "待发货",
                              image: "2",
                              callback: () =>
                                  Get.to(OrderListPage(), arguments: 1),
                            ),
                            _buildImageText(
                              title: "已发货",
                              image: "3",
                              callback: () =>
                                  Get.to(OrderListPage(), arguments: 2),
                            ),
                            _buildImageText(
                              title: "已完成",
                              image: "4",
                              callback: () =>
                                  Get.to(OrderListPage(), arguments: 3),
                            ),
                          ],
                        ),
                      ),
                      14.widthBox,
                    ],
                  )
                ],
              ),
            ),
            Container(
              margin: MarginPaddingUtil.only(top: 20, left: 20, right: 20),
              padding: MarginPaddingUtil.fromLTRB(
                  left: 31, top: 20, right: 31, bottom: 29),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20.width),
              ),
              child: Column(
                children: [
                  _buildTitle(
                    img: "group",
                    title: "我的推广",
                    color: Color(0xFFFFB39C),
                    rightIcon: "mine_right1",
                    rightStr: "团队详情",
                  ).addClickEvent(() => Get.to(MyGroupPage())),
                  51.heightBox,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      _buildTextText(
                          top: "团队业绩",
                          bottom:
                              "${controller.mineIndexBean?.teamAmount ?? 0}"),
                      _buildTextText(
                          top: "团队人数",
                          bottom: "${controller.mineIndexBean?.teamNum ?? 0}"),
                      _buildTextText(
                          top: "小区业绩",
                          bottom:
                              '${controller.mineIndexBean?.areaAmount ?? ""}'),
                    ],
                  ),
                ],
              ),
            ),
            20.heightBox,
            Container(
              margin: MarginPaddingUtil.only(top: 20, left: 20, right: 20),
              padding: MarginPaddingUtil.fromLTRB(
                  left: 31, top: 20, right: 31, bottom: 29),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20.width),
              ),
              child: Column(
                children: [
                  _buildTitle(
                    img: "red_letter",
                    title: "我的红包",
                    color: Color(0xFFFFB39C),
                    rightIcon: "mine_right1",
                    rightStr: "",
                    isShowRight: false,
                  ),
                  51.heightBox,
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      _buildTextText(
                          top: "总额度",
                          bottom:
                              (controller.mineIndexBean?.redPacketTotal ?? 0.0)
                                  .toStringAsFixed(2)),
                      _buildTextText(
                          top: "已释放",
                          bottom:
                              (controller.mineIndexBean?.redPacketReleased ??
                                      0.0)
                                  .toStringAsFixed(2)),
                      _buildTextText(
                          top: "待释放",
                          bottom:
                              (controller.mineIndexBean?.redPacketUnreleased ??
                                      0)
                                  .toStringAsFixed(2)),
                    ],
                  ),
                ],
              ),
            ),
            80.heightBox,
          ],
        ),
      ),
    );
  }

  Widget _buildScoreItem({
    required String title,
    required String image,
    required Color color,
    required String value,
    required VoidCallback callback,
  }) {
    return Expanded(
      child: Container(
        padding:
            MarginPaddingUtil.fromLTRB(left: 13, top: 10, right: 0, bottom: 10),
        decoration: BoxDecoration(
          color: color,
          borderRadius: BorderRadius.circular(15.width),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            Row(
              children: [
                ImageCommon(
                  "/icon/$image",
                  size: 32.width,
                ),
                8.widthBox,
                TextCommon(
                  title,
                  size: 20.sp,
                  lineHeight: 29.sp,
                )
              ],
            ),
            18.heightBox,
            TextCommon(
              value,
              size: 28.sp,
            )
          ],
        ),
      ).addClickEvent(callback),
    );
  }

  Widget _buildTitle(
      {required String img,
      required String title,
      required Color color,
      required String rightIcon,
      required String rightStr,
      bool isShowRight = true}) {
    return Row(
      children: [
        ImageCommon(
          "/icon/$img",
          size: 80.width,
        ),
        28.widthBox,
        TextCommon(
          title,
          size: 28.sp,
          fontWeight: FontWeight.bold,
        ),
        Spacer(),
        if (isShowRight)
          Container(
            padding: MarginPaddingUtil.symmetric(vertical: 10, horizontal: 18),
            decoration: BoxDecoration(
              color: color,
              borderRadius: BorderRadius.circular(50),
            ),
            child: Row(
              children: [
                TextCommon(
                  rightStr,
                  size: 24.sp,
                  lineHeight: 35.sp,
                ),
                12.widthBox,
                ImageCommon(
                  "/icon/$rightIcon",
                  size: 34.width,
                )
              ],
            ),
          )
      ],
    );
  }

  Widget _buildImageText(
      {required String title, required String image, VoidCallback? callback}) {
    return Column(
      children: [
        ImageCommon(
          "/icon/$image",
          size: 50.width,
        ),
        16.heightBox,
        TextCommon(
          title,
          size: 24.sp,
          lineHeight: 35.sp,
        )
      ],
    ).addClickEvent(callback);
  }

  Widget _buildTextText({required String top, required String bottom}) {
    return Column(
      children: [
        TextCommon(
          top,
          size: 24.sp,
        ),
        20.heightBox,
        TextCommon(
          bottom,
          size: 36.sp,
          fontWeight: FontWeight.bold,
        )
      ],
    );
  }

  Future<void> _jumpAssetsPage(int index) async {
    await Get.to(MyAssetsPage(),
        arguments: {"index": index, "id": controller.myAssetsBean});
    controller.getMyAssetsData();

    controller.getMineIndexData();
  }
}
